from rest_framework import serializers

from menus.models import Menu

class MenuSerializer(serializers.ModelSerializer):
    children = serializers.SerializerMethodField()
    class Meta:
        model = Menu
        fields = ['title', 'indexPath', 'icon', 'children']

    def get_children(self, obj):
        children = obj.children.all()
        if children.exists():
            return MenuSerializer(children, many=True).data
        return None